package com.primus.server.web.controller;

import com.primus.server.entity.user.User;
import com.primus.server.security.authentication.SelfUserDetails;
import com.primus.server.service.user.UserService;
import com.primus.server.web.ResponseBase;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

public abstract class BaseController {
    protected UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    protected <T> ResponseBase<T> success() {
        return this.success(null);
    }

    protected <T> ResponseBase<T> success(T t) {
        ResponseBase<T> responseBase = new ResponseBase<>();
        responseBase.setResult(t);
        return responseBase;
    }

    protected <S, T> T transfer(S source, T target, String... ignores) {
        BeanUtils.copyProperties(source, target, ignores);
        return target;
    }

    protected User getCurrentUser() {
        SelfUserDetails selfUserDetails = (SelfUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return this.userService.findByAccount(selfUserDetails.getUsername());
    }
}
